package com.winchain.chainbill.bill.biz.send.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.invoince.service.InvBasService;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.dao.CbAttDao;
import com.winchain.chainbill.bill.base.po.CbAtt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillInvoiceRejectCmd<T> implements ICommand<Bill<T>>, ICallback<Bill<T>, AcctTxRec> {
	@Autowired
	private CbAttDao cbAttDao;
	@Autowired
	private PubFileService pubFileService;
	@Autowired
	private InvBasService invBasService;

	public boolean callback(CallbackContext<Bill<T>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}

	public boolean execute(CommandContext<Bill<T>> context) throws Exception {
		String invType = context.getStringValue("invType");

		CbRec record = ((Bill) context.getSource()).getRec();
		String bizId = (String) context.getValue("bizId");
		if (!StringUtil.emptyAndNull(bizId)) {
			List<CbAtt> cbAtts = this.cbAttDao.selectDelCbAtt(bizId, record.getBillId(), "6", null);
			for (CbAtt cbAtt : cbAtts) {
				this.cbAttDao.deleteByPrimaryKey(cbAtt.getAttId());
				this.pubFileService.clearRelateId(cbAtt.getAttId(), "6");
				this.invBasService.deleteAndUpdateInv(cbAtt.getAttId(), invType);
			}
		}
		return false;
	}
}
